package com.antler.codelua;

import android.content.Context;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.antler.luajavalibrary.ScriptManager;
import com.antler.luajavalibrary.inf.IScriptManager;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener, IScriptManager.Callback {
    final static String TAG = "antler_" + MainActivity.class.getSimpleName();

    Button mExecute;
    Button mStop;
    Button mClear;
    CheckBox mScriptLoadFromFile;

    public EditText mSource;
    public TextView mStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "onCreate");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mExecute = findViewById(R.id.executeBtn);
        mExecute.setOnClickListener(this);

        mStop = findViewById(R.id.stopBtn);
        mStop.setOnClickListener(this);

        mClear = findViewById(R.id.clearBtn);
        mClear.setOnClickListener(this);

        mSource = findViewById(R.id.source);
        mSource.setOnLongClickListener(this);
        mSource.setText("print(\"hello lua\")\n");

        mStatus = findViewById(R.id.statusText);
        mStatus.setMovementMethod(ScrollingMovementMethod.getInstance());

        mScriptLoadFromFile = findViewById(R.id.fromFileCheckbox);

        // 初始化luajavalib
        ScriptManager.getInstance().init(MyApplication.getApplication());
        ScriptManager.getInstance().setCallback(this);
    }

    @Override
    protected void onResume() {
        Log.v(TAG, "onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.v(TAG, "onPause");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.v(TAG, "onDestroy");
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.executeBtn:
                mStatus.setText("");

                if (mScriptLoadFromFile.isChecked()) {
                    String path = getApplicationInfo().dataDir + "/script/demo.xsp";
                    ScriptManager.getInstance().startWithFile(path);
                } else {
                    String script = mSource.getText().toString();
                    ScriptManager.getInstance().start(script);
                }
                break;
            case R.id.stopBtn:
                ScriptManager.getInstance().stop();
                break;
            case R.id.clearBtn:
                clear();
                break;
        }
    }

    @Override
    public boolean onLongClick(View view) {
        return true;
    }

    protected void clear() {
        mSource.setText("");
    }

    @Override
    public void SM_print(String msg) {
        final String s = msg;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mStatus.append(s);
            }
        });
    }
}
